using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using Customs.Components.KDT.ThanhKhoan.Info;

namespace Customs.Components.KDT.ThanhKhoan
{
    public class HoSoThanhLyDangKy : HoSoThanhLyDangKyInfo
    {
        public void Load()
        {
            SqlDataReader reader = Select();
            if (reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal("So_TNDKDT"))) this.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("So_TNDKDT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_HQTN"))) this.MaHaiQuan = reader.GetString(reader.GetOrdinal("Ma_HQTN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) this.NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TN"))) this.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("Ngay_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_DV"))) this.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("Ma_DV"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TNCT"))) this.So_TNCT = reader.GetInt64(reader.GetOrdinal("So_TNCT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TNCT"))) this.Ngay_TNCT = reader.GetDateTime(reader.GetOrdinal("Ngay_TNCT"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXL"))) this.TrangThaiXL = reader.GetInt32(reader.GetOrdinal("TrangThaiXL"));
                reader.Close();
            }    
        }

        public void Load(HoSoThanhLyDangKyInfo hstl)
        {
            this._Ma_DV = hstl.MaDoanhNghiep;
            this._Ma_DVKB = hstl.Ma_DVKB;
            this._Ma_HQTN = hstl.MaHaiQuan;
            this._Nam_TN = hstl.NamTiepNhan;
            this._Ngay_TN = hstl.NgayTiepNhan;
            this._So_TNDKDT = hstl.SoTiepNhan;
            this._TrangThaiXL = hstl.TrangThaiXL;
            this.HoSoThanhLyCollection = hstl.HoSoThanhLyCollection;
            this.bkChuaThanhLy = hstl.BKChuaThanhLy;
            this.bkHuyBieuTang = hstl.BKHuyBieuTang;
            this.bkNhapKinhDoanh = hstl.BKNhapKinhDoanh;
            this.bkNopThue = hstl.BKNopThue;
            this.BKTaiXuat = hstl.BKTaiXuat;
            this.bkTamNopThue = hstl.BKTamNopThue;
            this.bkToKhaiNhap = hstl.BKToKhaiNhap;
            this.bkToKhaiXuat = hstl.BKToKhaiXuat;
            this.bkXuatGiaCong = hstl.BKXuatGiaCong;
        }

        public HoSoThanhLyDangKyInfo ExportToInfo()
        {
            HoSoThanhLyDangKyInfo hstl = new HoSoThanhLyDangKyInfo();
            hstl.TrangThaiCuaHSBenSXXK = this.TrangThaiCuaHSBenSXXK;
            hstl.LanThanhLy = this.lanthanhly;
            hstl.SoHoSoThanhLy = this.so_hs_tl;
            hstl.NgayBatDauTL = this.NgayBatDauTL;
            hstl.MaDoanhNghiep = this.MaDoanhNghiep;
            hstl.Ma_DVKB = this.Ma_DVKB;
            hstl.MaHaiQuan = this.MaHaiQuan;
            hstl.NamTiepNhan = this.NamTiepNhan;
            hstl.NgayTiepNhan = this.NgayTiepNhan;
            hstl.SoTiepNhan = this.SoTiepNhan;
            hstl.TrangThaiXL = this.TrangThaiXL;
            hstl.HoSoThanhLyCollection = this.HoSoThanhLyCollection;
            hstl.BKChuaThanhLy = BKNPLChuaThanhLyDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKHuyBieuTang = BKNPLHuyBieuTangDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKNhapKinhDoanh = BKNPLNhapKinhDoanhDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKNopThue = BKNPLNopThueDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKTaiXuat = BKNPLTaiXuatDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKTamNopThue = BKTamNopThueDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKToKhaiNhap = BKToKhaiNhapDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKToKhaiXuat = BKToKhaiXuatDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            hstl.BKXuatGiaCong = BKNPLXuatGiaCongDangKy.ExportToInfo(this.SoTiepNhan, this.MaHaiQuan, this.NamTiepNhan);
            return hstl;
        }

        public SqlDataReader Select()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            string sql = "select * from DHOSOTL where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";

            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;

            return SqlHelper.ExecuteReader(connectionString, CommandType.Text, sql, parameters);
        }

        public int Delete(SqlTransaction transaction)
        {
            string sql = "delete from DHOSOTL where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";
            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;            
            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text,sql, parameters);

        }

        public int Insert(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DHOSOTL (");
            query.Append("So_TNDKDT, ");
            query.Append("Ma_HQTN, ");
            query.Append("Nam_TN, ");
            query.Append("Ngay_TN, ");
            query.Append("So_TNCT, ");
            query.Append("Ngay_TNCT, ");
            query.Append("MA_DV, ");
            query.Append("Ma_DVKB, ");
            query.Append("TrangThaiXL, ");
            query.Append("AppSource ) ");

            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT, ");
            query.Append("@Ma_HQTN, ");
            query.Append("@Nam_TN, ");
            query.Append("@Ngay_TN, ");
            query.Append("@So_TNCT, ");
            query.Append("@Ngay_TNCT, ");
            query.Append("@MA_DV, ");
            query.Append("@Ma_DVKB, ");
            query.Append("@TrangThaiXL, ");
            query.Append("@AppSource )");

            SqlParameter[] parameters = new SqlParameter[10];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;

            parameters[3] = new SqlParameter("@Ngay_TN", SqlDbType.DateTime);
            parameters[3].Value = this.NgayTiepNhan;

            parameters[4] = new SqlParameter("@So_TNCT", SqlDbType.BigInt);
            parameters[4].Value = this.So_TNCT;

            parameters[5] = new SqlParameter("@Ngay_TNCT", SqlDbType.DateTime);
            parameters[5].Value = this.Ngay_TNCT;

            parameters[6] = new SqlParameter("@MA_DV", SqlDbType.VarChar, 14);
            parameters[6].Value = this.MaDoanhNghiep;

            parameters[7] = new SqlParameter("@Ma_DVKB", SqlDbType.VarChar, 14);
            parameters[7].Value = this.Ma_DVKB;

            parameters[8] = new SqlParameter("@TrangThaiXL", SqlDbType.Int);
            parameters[8].Value = 0;

            parameters[9] = new SqlParameter("@AppSource", SqlDbType.NVarChar, 50);
            parameters[9].Value = this.AppSource;

            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
   
        }

        public void LoadHoSoThanhLyCollection()
        {
            this.HoSoThanhLyCollection = HoSoThanhLy.LoadCollection(this.MaHaiQuan, this.SoTiepNhan, this.NamTiepNhan);
        }

        private long getSoMax(SqlTransaction transaction)
        {
            string sqlSelect = "SELECT SoMax FROM [DCAPSO_DHOSOTL] WHERE ([Ma_HQTN] = @MaHaiQuan) AND ([Nam_TN] = @NamTiepNhan)";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this.MaHaiQuan;

            parameters[1] = new SqlParameter("@NamTiepNhan", SqlDbType.SmallInt);
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this.NamTiepNhan;

            SqlDataReader reader = SqlHelper.ExecuteReader(transaction, CommandType.Text, sqlSelect, parameters);
            long result = 0;
            if (reader.Read())
            {
                result = Int64.Parse(reader["SoMax"].ToString());
            }
            reader.Close();
            return result;
        }

        private int updateSoMax(SqlTransaction transaction)
        {
            string sqlUpdate = "UPDATE [DCAPSO_DHOSOTL] SET [SoMax] = [SoMax] + 1 WHERE ([Ma_HQTN] = @MaHaiQuan) AND ([Nam_TN] = @NamTiepNhan)";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this.MaHaiQuan;

            parameters[1] = new SqlParameter("@NamTiepNhan", SqlDbType.SmallInt);
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this.NamTiepNhan;

            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, sqlUpdate, parameters);
        }

        private int insertSoMax(SqlTransaction transaction)
        {
            string sqlInsert = "INSERT INTO [DCAPSO_DHOSOTL] ([Ma_HQTN],	[Nam_TN], [SoMax]) VALUES (@MaHaiQuan, @NamTiepNhan, 1)";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this.MaHaiQuan;

            parameters[1] = new SqlParameter("@NamTiepNhan", SqlDbType.SmallInt);
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this.NamTiepNhan;

            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, sqlInsert, parameters);
        }

        //-----------------------------------------------------------------------------------------
        public long GetSoTiepNhan(SqlTransaction transaction)
        {
            long somax = this.getSoMax(transaction);
            if (somax == 0)
            {
                this.insertSoMax(transaction);
                somax = 1;
            }
            else
            {
                this.updateSoMax(transaction);
                somax = somax + 1;
            }
            return somax;
        }
        #region Webservice
        public static long KhaiBao(HoSoThanhLyDangKyInfo hoso)
        {
            long sotiepnhan = 0;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transactionKDT = conn.BeginTransaction();
                try
                {
                    HoSoThanhLyDangKy hsKhaiBao = new HoSoThanhLyDangKy();
                    hsKhaiBao.Load(hoso);
                    if (hsKhaiBao.SoTiepNhan > 0)
                    {
                        hsKhaiBao.Load();
                        if(hsKhaiBao.TrangThaiXL==1)
                            throw new Exception("123DaDuyet321");
                        HuyThongTin(hsKhaiBao.SoTiepNhan, hsKhaiBao.MaHaiQuan, hsKhaiBao.NamTiepNhan, transactionKDT);
                        hsKhaiBao.Load(hoso);
                    }
                    hsKhaiBao.NgayTiepNhan = DateTime.Today;
                    hsKhaiBao.NamTiepNhan = (short)hsKhaiBao.NgayTiepNhan.Year;
                    hsKhaiBao.SoTiepNhan = hsKhaiBao.GetSoTiepNhan(transactionKDT);
                    //cap nhat ho so thanh ly
                    hsKhaiBao.Insert(transactionKDT);
                    //cap nhat danh sach bang ke 
                    foreach (HoSoThanhLyInfo info in hsKhaiBao.HoSoThanhLyCollection)
                    {
                        HoSoThanhLy hstl = new HoSoThanhLy();
                        hstl.Load(info);
                        hstl.SoTiepNhan=hsKhaiBao.SoTiepNhan;
                        hstl.NamTiepNhan=hsKhaiBao.NamTiepNhan;                        
                        hstl.Insert(transactionKDT);
                    }
                    //cap nhat thong tin tung bang ke va thong tin hang cua bang ke

                    //bang ke npl chua thanh ly
                    if (hsKhaiBao.BKChuaThanhLy != null)
                    {
                        BKNPLChuaThanhLyDangKy bkChuaThanhLy = new BKNPLChuaThanhLyDangKy();
                        bkChuaThanhLy.Load(hsKhaiBao.BKChuaThanhLy);
                        bkChuaThanhLy.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkChuaThanhLy.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkChuaThanhLy.TrangThaiXL = 0;
                        bkChuaThanhLy.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkChuaThanhLy.insertFull(transactionKDT);
                    }
                    //bang ke npl chua thanh ly
                    if (hsKhaiBao.BKHuyBieuTang != null)
                    {
                        BKNPLHuyBieuTangDangKy bkHuyBieuTang = new BKNPLHuyBieuTangDangKy();
                        bkHuyBieuTang.Load(hsKhaiBao.BKHuyBieuTang);
                        bkHuyBieuTang.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkHuyBieuTang.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkHuyBieuTang.TrangThaiXL = 0;
                        bkHuyBieuTang.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkHuyBieuTang.insertFull(transactionKDT);
                    }
                    // bang ke nhap kinh doanh
                    if (hsKhaiBao.BKNhapKinhDoanh != null)
                    {
                        BKNPLNhapKinhDoanhDangKy bkNhapKinhDoanh = new BKNPLNhapKinhDoanhDangKy();
                        bkNhapKinhDoanh.Load(hsKhaiBao.BKNhapKinhDoanh);
                        bkNhapKinhDoanh.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkNhapKinhDoanh.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkNhapKinhDoanh.TrangThaiXL = 0;
                        bkNhapKinhDoanh.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkNhapKinhDoanh.insertFull(transactionKDT);
                    }
                    // bang ke npl nop thue tieu thu noi dia
                    if (hsKhaiBao.BKNopThue != null)
                    {
                        BKNPLNopThueDangKy bkNopThue = new BKNPLNopThueDangKy();
                        bkNopThue.Load(hsKhaiBao.BKNopThue);
                        bkNopThue.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkNopThue.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkNopThue.TrangThaiXL = 0;
                        bkNopThue.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkNopThue.insertFull(transactionKDT);
                    }
                    // bang ke tai xuat
                    if (hsKhaiBao.BKTaiXuat != null)
                    {
                        BKNPLTaiXuatDangKy bkTaixuat = new BKNPLTaiXuatDangKy();
                        bkTaixuat.Load(hsKhaiBao.BKTaiXuat);
                        bkTaixuat.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkTaixuat.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkTaixuat.TrangThaiXL = 0;
                        bkTaixuat.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkTaixuat.insertFull(transactionKDT);
                    }
                    // bang ke tam nop thue
                    if (hsKhaiBao.BKTamNopThue != null)
                    {
                        BKTamNopThueDangKy bkTamNopThue = new BKTamNopThueDangKy();
                        bkTamNopThue.Load(hsKhaiBao.BKTamNopThue);
                        bkTamNopThue.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkTamNopThue.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkTamNopThue.TrangThaiXL = 0;
                        bkTamNopThue.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkTamNopThue.insertFull(transactionKDT);
                    }
                    // bang ke nhap to khai nhap
                    if (hsKhaiBao.BKToKhaiNhap != null)
                    {
                        BKToKhaiNhapDangKy bkToKhaiNhap = new BKToKhaiNhapDangKy();
                        bkToKhaiNhap.Load(hsKhaiBao.BKToKhaiNhap);
                        bkToKhaiNhap.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkToKhaiNhap.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkToKhaiNhap.TrangThaiXL = 0;
                        bkToKhaiNhap.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkToKhaiNhap.insertFull(transactionKDT);
                    }
                    // bang ke to khai xuat
                    if (hsKhaiBao.BKToKhaiXuat != null)
                    {
                        BKToKhaiXuatDangKy bkToKhaiXuat = new BKToKhaiXuatDangKy();
                        bkToKhaiXuat.Load(hsKhaiBao.BKToKhaiXuat);
                        bkToKhaiXuat.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkToKhaiXuat.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkToKhaiXuat.TrangThaiXL = 0;
                        bkToKhaiXuat.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkToKhaiXuat.insertFull(transactionKDT);
                    }
                    // bang ke to khai xuat
                    if (hsKhaiBao.BKXuatGiaCong != null)
                    {
                        BKNPLXuatGiaCongDangKy bkXuatGiaCong = new BKNPLXuatGiaCongDangKy();
                        bkXuatGiaCong.Load(hsKhaiBao.BKXuatGiaCong);
                        bkXuatGiaCong.SoTiepNhan = hsKhaiBao.SoTiepNhan;
                        bkXuatGiaCong.NamTiepNhan = hsKhaiBao.NamTiepNhan;
                        bkXuatGiaCong.TrangThaiXL = 0;
                        bkXuatGiaCong.NgayTiepNhan = hsKhaiBao.NgayTiepNhan;
                        bkXuatGiaCong.insertFull(transactionKDT);
                    }
                    transactionKDT.Commit();
                    sotiepnhan = hsKhaiBao.SoTiepNhan;
                }
                catch (Exception ex)
                {
                    transactionKDT.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            return sotiepnhan;
        }

        //-----------------------------------------------------------------
        public static void HuyThongTin(long soTiepNhan, string maHaiQuan, short namTiepNhan, SqlTransaction transaction)
        {
            BKNPLChuaThanhLyDangKy bkChuaThanhLy = new BKNPLChuaThanhLyDangKy();         
            bkChuaThanhLy.SoTiepNhan = soTiepNhan;
            bkChuaThanhLy.NamTiepNhan = namTiepNhan;
            bkChuaThanhLy.MaHaiQuan = maHaiQuan;
            bkChuaThanhLy.deleteFull(transaction);

            //bang ke npl huy bieu tang
            BKNPLHuyBieuTangDangKy bkHuyBieuTang = new BKNPLHuyBieuTangDangKy();
            bkHuyBieuTang.SoTiepNhan = soTiepNhan;
            bkHuyBieuTang.NamTiepNhan = namTiepNhan;
            bkHuyBieuTang.MaHaiQuan = maHaiQuan;
            bkHuyBieuTang.deleteFull(transaction);

            // bang ke nhap kinh doanh
            BKNPLNhapKinhDoanhDangKy bkNhapKinhDoanh = new BKNPLNhapKinhDoanhDangKy();
            bkNhapKinhDoanh.SoTiepNhan = soTiepNhan;
            bkNhapKinhDoanh.NamTiepNhan = namTiepNhan;
            bkNhapKinhDoanh.MaHaiQuan = maHaiQuan;
            bkNhapKinhDoanh.deleteFull(transaction);

            // bang ke npl nop thue tieu thu noi dia
            BKNPLNopThueDangKy bkNopThue = new BKNPLNopThueDangKy();
            bkNopThue.SoTiepNhan = soTiepNhan;
            bkNopThue.NamTiepNhan = namTiepNhan;
            bkNopThue.MaHaiQuan = maHaiQuan;
            bkNopThue.deleteFull(transaction);

            // bang ke tai xuat
            BKNPLTaiXuatDangKy bkTaixuat = new BKNPLTaiXuatDangKy();
            bkTaixuat.SoTiepNhan = soTiepNhan;
            bkTaixuat.NamTiepNhan = namTiepNhan;
            bkTaixuat.MaHaiQuan = maHaiQuan;
            bkTaixuat.deleteFull(transaction);

            // bang ke tam nop thue
            BKTamNopThueDangKy bkTamNopThue = new BKTamNopThueDangKy();
            bkTamNopThue.SoTiepNhan = soTiepNhan;
            bkTamNopThue.NamTiepNhan = namTiepNhan;
            bkTamNopThue.MaHaiQuan = maHaiQuan;
            bkTamNopThue.deleteFull(transaction);

            // bang ke nhap to khai nhap
            BKToKhaiNhapDangKy bkToKhaiNhap = new BKToKhaiNhapDangKy();
            bkToKhaiNhap.SoTiepNhan = soTiepNhan;
            bkToKhaiNhap.NamTiepNhan = namTiepNhan;
            bkToKhaiNhap.MaHaiQuan = maHaiQuan;
            bkToKhaiNhap.deleteFull(transaction);

            // bang ke to khai xuat
            BKToKhaiXuatDangKy bkToKhaiXuat = new BKToKhaiXuatDangKy();
            bkToKhaiXuat.SoTiepNhan = soTiepNhan;
            bkToKhaiXuat.NamTiepNhan = namTiepNhan;
            bkToKhaiXuat.MaHaiQuan = maHaiQuan;
            bkToKhaiXuat.deleteFull(transaction);


            // bang ke to khai xuat
            BKNPLXuatGiaCongDangKy bkXuatGiaCong = new BKNPLXuatGiaCongDangKy();
            bkXuatGiaCong.SoTiepNhan = soTiepNhan;
            bkXuatGiaCong.NamTiepNhan = namTiepNhan;
            bkXuatGiaCong.MaHaiQuan = maHaiQuan;
            bkXuatGiaCong.deleteFull(transaction);
            //xoa ho so thanh ly
            HoSoThanhLyDangKy hsDangKy = new HoSoThanhLyDangKy();
            hsDangKy.SoTiepNhan = soTiepNhan;
            hsDangKy.NamTiepNhan = namTiepNhan;
            hsDangKy.MaHaiQuan = maHaiQuan;
            HoSoThanhLy.DeleteByForeignKey(transaction, maHaiQuan, soTiepNhan, namTiepNhan);
            hsDangKy.Delete(transaction);
        }
        public static bool HuyThongTinKhaiBao(long soTiepNhan, string maHaiQuan, short namTiepNhan)
        {
            bool ret;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction();
                try
                {
                    HuyThongTin(soTiepNhan, maHaiQuan, namTiepNhan, transaction);
                    transaction.Commit();
                    ret = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return ret;
        }
        private  DataSet GetSoHoSoThanhLy()
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", this.MaHaiQuan);
            string query = "SELECT     MAX(SO_HS_TL) AS sohosothanhly, LAN_TL,NGAY_BD_TL,TRANG_THAI " +
                           "FROM         DLANTHANHLY "+
                           "WHERE     (MA_DV = @Ma_DV) AND (MA_HQ = @Ma_HQ) " +
                           "GROUP BY MA_DV, LAN_TL,NGAY_BD_TL,TRANG_THAI";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this.MaHaiQuan; ;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this.MaDoanhNghiep;    

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
        }
        public static HoSoThanhLyDangKyInfo RequestHoSoThanhLy(long soTiepNhan, string maHaiQuan, short namTiepNhan)
        {            
            HoSoThanhLyDangKy hdClass = new HoSoThanhLyDangKy();
            hdClass.SoTiepNhan = soTiepNhan;
            hdClass.NamTiepNhan = namTiepNhan;
            hdClass.MaHaiQuan = maHaiQuan;            
            hdClass.Load();
            hdClass.LoadHoSoThanhLyCollection();
            if (hdClass.TrangThaiXL == 1)
            {
                DataSet ds = hdClass.GetSoHoSoThanhLy();
                hdClass.SoHoSoThanhLy = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
                hdClass.LanThanhLy = Convert.ToInt32(ds.Tables[0].Rows[0][1].ToString());
                hdClass.NgayBatDauTL = Convert.ToDateTime(ds.Tables[0].Rows[0][2].ToString());
                hdClass.TrangThaiCuaHSBenSXXK = Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString());
            }
            return hdClass.ExportToInfo();
        }

        public static HoSoThanhLyInfoCollection RequestStatusBangKe(long soTiepNhan, string maHaiQuan, short namTiepNhan)
        {
            HoSoThanhLyDangKy hdClass = new HoSoThanhLyDangKy();
            hdClass.SoTiepNhan = soTiepNhan;
            hdClass.NamTiepNhan = namTiepNhan;
            hdClass.MaHaiQuan = maHaiQuan;            
            hdClass.LoadHoSoThanhLyCollection();
            foreach (HoSoThanhLy hstl in hdClass.HoSoThanhLyCollection)
            {
                switch (hstl.MaBangKe.Trim())
                {
                    case "DTLNPLCHUATL":
                        {
                            BKNPLChuaThanhLyDangKy bkChuaThanhLy=new BKNPLChuaThanhLyDangKy();
                            bkChuaThanhLy.SoTiepNhan=hstl.SoTiepNhan;
                            bkChuaThanhLy.NamTiepNhan=hstl.NamTiepNhan;
                            bkChuaThanhLy.MaHaiQuan=hstl.MaHaiQuan;
                            bkChuaThanhLy.Load();
                            hstl.TrangThaiBangKe = bkChuaThanhLy.TrangThaiXL;
                        }
                        break;
                    case "DTLCHITIETNT":
                        {
                            BKTamNopThueDangKy bkTamNopThue = new BKTamNopThueDangKy();
                            bkTamNopThue.SoTiepNhan = hstl.SoTiepNhan;
                            bkTamNopThue.NamTiepNhan = hstl.NamTiepNhan;
                            bkTamNopThue.MaHaiQuan = hstl.MaHaiQuan;
                            bkTamNopThue.Load();
                            hstl.TrangThaiBangKe = bkTamNopThue.TrangThaiXL;
                        }
                        break;
                    case "DTLNPLNKD":
                        {
                            BKNPLNhapKinhDoanhDangKy bkNhapKinhDoanh = new BKNPLNhapKinhDoanhDangKy();
                            bkNhapKinhDoanh.SoTiepNhan = hstl.SoTiepNhan;
                            bkNhapKinhDoanh.NamTiepNhan = hstl.NamTiepNhan;
                            bkNhapKinhDoanh.MaHaiQuan = hstl.MaHaiQuan;
                            bkNhapKinhDoanh.Load();
                            hstl.TrangThaiBangKe = bkNhapKinhDoanh.TrangThaiXL;
                        }
                        break;
                    case "DTLNPLNT":
                        {
                            BKNPLNopThueDangKy bkNopThue = new BKNPLNopThueDangKy();
                            bkNopThue.SoTiepNhan = hstl.SoTiepNhan;
                            bkNopThue.NamTiepNhan = hstl.NamTiepNhan;
                            bkNopThue.MaHaiQuan = hstl.MaHaiQuan;
                            bkNopThue.Load();
                            hstl.TrangThaiBangKe = bkNopThue.TrangThaiXL;
                        }
                        break;
                    case "DTLNPLTX":
                        {
                            BKNPLTaiXuatDangKy bkTaiXuat = new BKNPLTaiXuatDangKy();
                            bkTaiXuat.SoTiepNhan = hstl.SoTiepNhan;
                            bkTaiXuat.NamTiepNhan = hstl.NamTiepNhan;
                            bkTaiXuat.MaHaiQuan = hstl.MaHaiQuan;
                            bkTaiXuat.Load();
                            hstl.TrangThaiBangKe = bkTaiXuat.TrangThaiXL;
                        }
                        break;
                    case "DTLNPLXGC":
                        {
                            BKNPLXuatGiaCongDangKy bkXuatGiaCong = new BKNPLXuatGiaCongDangKy();
                            bkXuatGiaCong.SoTiepNhan = hstl.SoTiepNhan;
                            bkXuatGiaCong.NamTiepNhan = hstl.NamTiepNhan;
                            bkXuatGiaCong.MaHaiQuan = hstl.MaHaiQuan;
                            bkXuatGiaCong.Load();
                            hstl.TrangThaiBangKe = bkXuatGiaCong.TrangThaiXL;
                        }
                        break;
                    case "DTLNPLXH":
                        {
                            BKNPLHuyBieuTangDangKy bkHuyBieuTang = new BKNPLHuyBieuTangDangKy();
                            bkHuyBieuTang.SoTiepNhan = hstl.SoTiepNhan;
                            bkHuyBieuTang.NamTiepNhan = hstl.NamTiepNhan;
                            bkHuyBieuTang.MaHaiQuan = hstl.MaHaiQuan;
                            bkHuyBieuTang.Load();
                            hstl.TrangThaiBangKe = bkHuyBieuTang.TrangThaiXL;
                        }
                        break; ;
                    case "DTLTKN":
                        {
                            BKToKhaiNhapDangKy bkToKhaiNhap = new BKToKhaiNhapDangKy();
                            bkToKhaiNhap.SoTiepNhan = hstl.SoTiepNhan;
                            bkToKhaiNhap.NamTiepNhan = hstl.NamTiepNhan;
                            bkToKhaiNhap.MaHaiQuan = hstl.MaHaiQuan;
                            bkToKhaiNhap.Load();
                            hstl.TrangThaiBangKe = bkToKhaiNhap.TrangThaiXL;
                        }
                        break;
                    case "DTLTKX":
                        {
                            BKToKhaiXuatDangKy bkToKhaiXuat = new BKToKhaiXuatDangKy();
                            bkToKhaiXuat.SoTiepNhan = hstl.SoTiepNhan;
                            bkToKhaiXuat.NamTiepNhan = hstl.NamTiepNhan;
                            bkToKhaiXuat.MaHaiQuan = hstl.MaHaiQuan;
                            bkToKhaiXuat.Load();
                            hstl.TrangThaiBangKe = bkToKhaiXuat.TrangThaiXL;
                        }
                        break;
                    default:
                        //this.bkNPLTCUCollection = BKNPLTuCungUng.SelectCollectionBy_BangKeHoSoThanhLy_ID(this.ID);
                        break;

                }
            }
            return hdClass.HoSoThanhLyCollection;
        }

        
        #endregion 
    }
}

